﻿#region Copyright and License Notice
// Copyright (C)2010-2016 - Rob Levine and other contributors
// http://configgen.codeplex.com
// 
// This file is part of ConfigGen.
// 
// ConfigGen is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// ConfigGen is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License and 
// the GNU Lesser General Public License along with ConfigGen.  
// If not, see <http://www.gnu.org/licenses/>
#endregion

using System.IO;
using System.Text;
using ConfigGen.Core.ConfigurationSettings;
using ConfigGen.Core.TemplateProcessing;
using NUnit.Framework;

namespace ConfigGen.Core.Tests.TemplateProcessing
{
    [TestFixture]
    public class TemplateProcessorTests
    {
        [Test]
        public void ProcessTemplate001_EchoesTemplateWithNoConfigGenAttributes()
        {
            const string xml = @"<root>
  <child key=""value"" />
</root>";
            using (var rawTemplateStream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
            using (var processedTemplateStream = new MemoryStream())
            {
                var processor = new TemplateProcessor();
                var machineSettings = new MachineConfigurationSettings("machineName", "configFilePath", new ConfigurationSetting[0]);
                processor.ProcessTemplate(rawTemplateStream, processedTemplateStream, machineSettings);

                using (var reader = new StreamReader(processedTemplateStream))
                {
                    string result = reader.ReadToEnd();
                    Assert.AreEqual(xml, result);
                }
            }
        }

        [Test]
        public void ProcessTemplate002_EchoesXmlDeclaration()
        {
            const string xml = @"<?xml version=""1.0"" encoding=""utf-8""?><root>
  <child key=""value"" />
</root>";

            using (var rawTemplateStream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
            using (var processedTemplateStream = new MemoryStream())
            {
                var processor = new TemplateProcessor();
                var machineSettings = new MachineConfigurationSettings("machineName", "configFilePath", new ConfigurationSetting[0]);

                processor.ProcessTemplate(rawTemplateStream, processedTemplateStream, machineSettings);

                using (var reader = new StreamReader(processedTemplateStream))
                {
                    string result = reader.ReadToEnd();
                    Assert.AreEqual(xml, result);
                }
            }
        }

        [Test]
        public void ProcessTemplate003a_ConfigGenNamespaceRemoved()
        {
            const string xml = @"<root xmlns:cg=""http://roblevine.co.uk/Namespaces/ConfigGen/1/0/"">
  <child key=""value""/>
</root>";

            using (var rawTemplateStream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
            using (var processedTemplateStream = new MemoryStream())
            {
                var processor = new TemplateProcessor();
                var machineSettings = new MachineConfigurationSettings("machineName", "configFilePath", new ConfigurationSetting[0]);
                processor.ProcessTemplate(rawTemplateStream, processedTemplateStream, machineSettings);

                using (var reader = new StreamReader(processedTemplateStream))
                {
                    string result = reader.ReadToEnd();
                    Assert.IsFalse(result.Contains(TemplateProcessor.ConfigGenXmlNamespace));
                }
            }
        }

        [Test]
        public void ProcessTemplate003b_OtherNamespacesLeftIntact()
        {
            const string xml = @"<root xmlns:cg=""http://roblevine.co.uk/NotConfigGen"">
  <child key=""value""/>
</root>";

            using (var rawTemplateStream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
            using (var processedTemplateStream = new MemoryStream())
            {
                var processor = new TemplateProcessor();
                var machineSettings = new MachineConfigurationSettings("machineName", "configFilePath", new ConfigurationSetting[0]);
                processor.ProcessTemplate(rawTemplateStream, processedTemplateStream, machineSettings);

                using (var reader = new StreamReader(processedTemplateStream))
                {
                    string result = reader.ReadToEnd();
                    Assert.IsTrue(result.Contains("http://roblevine.co.uk/NotConfigGen"));
                }
            }
        }

//        [Test]
//        public void ProcessTemplate004()
//        {
//            const string xml = @"<root xmlns:cg=""http://roblevine.co.uk/Namespaces/ConfigGen/1/0/"">
//<node cg:configGenAttribute='as'/>
//  <cg:configGenNode key=""value""/>
//</root>";

//            using (var rawTemplateStream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
//            using (var processedTemplateStream = new MemoryStream())
//            {
//                var processor = new TemplateProcessor();
//                var machineSettings = new MachineConfigurationSettings("machineName", "configFilePath", new ConfigurationSetting[0]);
//                processor.ProcessTemplate(rawTemplateStream, processedTemplateStream, machineSettings);

//                using (var reader = new StreamReader(processedTemplateStream))
//                {
//                    string result = reader.ReadToEnd();
//                    Assert.IsTrue(result.Contains("http://roblevine.co.uk/NotConfigGen"));
//                }
//            }
//        }
    }
}
